package com.example.demo.controller;

import com.example.demo.entity.Category;
import com.example.demo.service.ICategoryService;


import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import com.example.demo.utils.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品模块Controller
 * 
 * @author ruoyi
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/system/category")
public class CategoryController
{
    @Autowired
    private ICategoryService categoryService;

    @PassToken
    @GetMapping("/list")
    public ServiceResponse list(Category category,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "11")int pageSize)
    {
        PageResult pageResult = categoryService.selectCategoryList(category,pageNum,pageSize);
        return ServiceResponse.createBySuccess(200,pageResult,"获取成功");
    }
    @PassToken
    @GetMapping("/list1")
    public ServiceResponse list1(Category category)
    {
        List<Category> list = categoryService.selectCategoryGoodsList(category);
        return ServiceResponse.createBySuccess(200,list,"获取成功");
    }



    /**
     * 获取商品模块详细信息
     */
    @GetMapping(value = "/{cid}")
    public ServiceResponse getInfo(@PathVariable("cid") Long cid)
    {
        return ServiceResponse.createBySuccess(200,categoryService.selectCategoryByCid(cid),"获取成功");
    }

    /**
     * 新增商品模块
     */

    @PostMapping
    public ServiceResponse add(@RequestBody Category category)
    {
        int i =categoryService.insertCategory(category);
        if(i>0){
            return ServiceResponse.createBySuccess(200,"","添加成功");
        }else{
            return  ServiceResponse.createByFail(500,"添加失败");

        }
    }

    /**
     * 修改商品模块
     */

    @PutMapping
    public ServiceResponse edit(@RequestBody Category category)
    {
        int i =categoryService.updateCategory(category);
        if(i>0){
            return ServiceResponse.createBySuccess(200,"","更新成功");
        }else{
            return  ServiceResponse.createByFail(500,"更新失败");

        }
    }

    /**
     * 删除商品模块
     */

	@DeleteMapping("/{cids}")
    public ServiceResponse remove(@PathVariable Long[] cids)
    {
        int i =categoryService.deleteCategoryByCids(cids);
        if(i>0){
            return ServiceResponse.createBySuccess(200,"","删除成功");
        }else{
            return  ServiceResponse.createByFail(500,"删除失败");

        }

    }
}
